import pygame

"""
计时器类
"""
class Timer:
    # duration持续时间
    def __init__(self,duration,func = None):
        self.duration = duration
        self.func= func
        self.start_time = 0
        # 默认状态下未激活
        self.active = False

    # 计时器已激活
    def activate(self):
        self.active = True
        self.start_time = pygame.time.get_ticks()

    # 未激活
    def deactivate(self):
        self.active = False
        self.start_time = 0

    def update(self):
        current_time = pygame.time.get_ticks()
        if current_time - self.start_time >= self.duration:
            # 如果函数存在则调用函数
            if self.func and self.start_time != 0:
                self.func()
            self.deactivate()